package com.passionfruit.module.service.impl;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.passionfruit.common.utils.Query;
import com.passionfruit.common.utils.UuidUtils;
import com.passionfruit.module.entity.BlogArticle;
import com.passionfruit.module.mapper.BlogArticleMapper;
import com.passionfruit.module.service.IBlogArticleCateLinkService;
import com.passionfruit.module.service.IBlogArticleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2017-11-06
 */
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements IBlogArticleService {

    @Autowired
    private BlogArticleMapper blogArticleMapper;

    @Autowired
    private IBlogArticleCateLinkService blogArticleCateLinkService;

    @Override
    public List<Map<String, Object>> selectBlogArticlePage(Pagination page, Map<String, Object> params) {
        return blogArticleMapper.selectBlogArticlePage(page,params);
    }

    @Override
    public List<BlogArticle> queryList(Query query) {
        return blogArticleMapper.queryList(query);
    }

    @Override
    public BlogArticle queryObject(Integer id) {
        return blogArticleMapper.queryObject(id);
    }

    @Override
    public void save(BlogArticle blogArticle) {
        blogArticle.setArticleKey(UuidUtils.getUUID());
        System.out.println(UuidUtils.getUUID().length());
        blogArticle.setCreateTime(Convert.toInt(new Date()));

        super.insert(blogArticle);

        //保存文章与分类关系
        blogArticleCateLinkService.saveOrUpdate(blogArticle.getId(), blogArticle.getCateId());
    }

    @Override
    public void update(BlogArticle blogArticle) {
        blogArticle.setUpdateTime(Convert.toInt(new Date()));
        super.updateById(blogArticle);
        //保存文章与分类关系
        blogArticleCateLinkService.saveOrUpdate(blogArticle.getId(), blogArticle.getCateId());
    }
}
